package com.qzn.backend.controller;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qzn.backend.dto.GetReportDto;
import com.qzn.backend.dto.item.GetReportResultItemDto;
import com.qzn.backend.dto.item.StatusItemDto;
import com.qzn.backend.service.ReportService;

@RequestMapping(value = "reportpage")
@Controller
public class ReportPageController {

	private final Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private ReportService reportService;

	/**
	 * 获取报告<br>
	 * 文本信息直接获取<br>
	 * 图片和语音单独获取/report/getmedia
	 * 
	 * @param user
	 * @param pageno
	 * @param size
	 * @return
	 */
	@RequestMapping(value = "getreport")
	public String getReport(GetReportDto reportDto, Model model) {

		try {

			reportDto.setOffset();
			reportDto.setUser(URLDecoder.decode(reportDto.getUser(), "utf-8"));

			List<StatusItemDto> statusList = new ArrayList<StatusItemDto>();
			statusList.add(new StatusItemDto("", "All"));
			statusList.add(new StatusItemDto("0", "Unhandled"));
			statusList.add(new StatusItemDto("1", "Handled"));

			// 获取报告总数
			Integer cnt = reportService.getReportCount(reportDto);
			reportDto.setCnt(cnt == null ? 0 : cnt);

			// 获取报告列表
			List<GetReportResultItemDto> items = reportService
					.getReport(reportDto);

			model.addAttribute("items", items);
			model.addAttribute("statusList", statusList);
			model.addAttribute("cnt", cnt);

		} catch (Exception e) {
			log.error("get report error" + e);
		}

		return "home2";

	}

	/**
	 * 审核
	 * 
	 * @param user
	 * @param pageno
	 * @param size
	 * @return
	 */
	@RequestMapping(value = "verify")
	public String verify(String id, String redirecturl) {

		reportService.verify(id);

		return "redirect:getreport";

	}

}
